package visualizer.drawable;

import java.util.Map;

import processing.core.PGraphics;
import visualizer.data.DataSource;

public class DrawableSwitcher<T> implements Drawable {
	private Map<T, Drawable> drawables;
	private DataSource<T> selected = new DataSource<T>(null);
	private boolean enabled = true;
	
	public DrawableSwitcher(Map<T, Drawable> drawables)
	{
		this.drawables = drawables;
	}
	
	public DrawableSwitcher<T> setSelectedKey(T selected)
	{
		if (getSelectedDrawable() != null) {
			getSelectedDrawable().setEnabled(false);
		}
		this.selected.set(selected);
		if (getSelectedDrawable() != null) {
			getSelectedDrawable().setEnabled(true);
		}
		return this;
	}
	
	public DataSource<T> getDataSource()
	{
		return selected;
	}
	
	public T getSelectedKey()
	{
		return selected.get();
	}
	
	public Drawable getSelectedDrawable()
	{
		return drawables.get(getSelectedKey());
	}
	
	public Drawable getDrawable(T key)
	{
		return drawables.get(key);
	}
	
	public DrawableSwitcher<T> setDrawable(T key, Drawable d)
	{
		drawables.put(key, d);
		return this;
	}
	
	public void draw(PGraphics g)
	{
		if (getSelectedDrawable() != null) {
			getSelectedDrawable().draw(g);
		}
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean status) {
		this.enabled = status;
	}
}
